package com.yuexunit.operationman.nbsy.module.OperationInventory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationInventory.adapter.GoodsInventoryAdapter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.InventoryBean;
import com.yuexunit.operationman.nbsy.module.OperationInventory.model.impl.InventoryModel;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.IInventoryPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.impl.InventoryPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.ui.view.IInventoryView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/14 0014.
 */

public class Act_OperationInventory_GoodsList extends BaseKJActivity implements IInventoryView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.listview)
    FixedSwipeListView listview;

    GoodsInventoryAdapter adapter;
    ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> list = new ArrayList<>();

    IInventoryPresenter presenter;
    LoadingDialog loadingDialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_operationinventory_goodslist);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(aty);
        presenter = new InventoryPresenter(this,new InventoryModel());
        list =(ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>) getIntent().getExtras().get("goods");
        if(getIntent().getExtras().getBoolean("isFirst"))
        {
            for (int i =0;i<list.size();i++)
            {
                Pattern pattern = Pattern.compile("[+-]\\d+");
                Matcher matcher = pattern.matcher(list.get(i).getInAdd());
                int all =0;
                while(matcher.find()) {
                    if(matcher.group().startsWith("+"))
                    {
                        all+= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
                    }
                    else if(matcher.group().startsWith("-"))
                    {
                        all-= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
                    }
                }
                list.get(i).setBeforeCloseBody(list.get(i).getBeforeCheck()+all);
                list.get(i).setAfterCloseBody(list.get(i).getBeforeCheck()+all);
                list.get(i).setAfterSkin(list.get(i).getBeforeCheck()+all);
            }
        }
        adapter = new GoodsInventoryAdapter(listview,list,R.layout.item_operationinventory_goods);
        listview.setAdapter(adapter);
    }

    private void setTitle() {
        tvSubmit.setText("确认");
        tvPatientName.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientName());
        if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INVENTORYBEAN.getObject().getCheck().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }


    @OnClick({R.id.titlebar_docadvice_img_back, R.id.tv_submit, R.id.add, R.id.edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.tv_submit:
                insertContent();
                break;
            case R.id.add:
                Bundle bundle_add = new Bundle();
                bundle_add.putSerializable("goods",list);
                Intent intent_add= new Intent(aty, Act_OperationInventory_AddList.class);
                intent_add.putExtras(bundle_add);
                startActivityForResult(intent_add,102);
                break;
            case R.id.edit:
                Bundle bundle_edt = new Bundle();
                bundle_edt.putSerializable("goods",list);
                Intent intent_edit= new Intent(aty, Act_OperationInventory_EditList.class);
                intent_edit.putExtras(bundle_edt);
                startActivityForResult(intent_edit,101);
                break;
        }
    }

    private void insertContent() {
        if(AppConfig.INVENTORYBEAN.getObject().getCheckItemList()!=null)
        AppConfig.INVENTORYBEAN.getObject().getCheckItemList().clear();

        AppConfig.INVENTORYBEAN.getObject().setCheckItemList(list);
        if (AppConfig.INVENTORYBEAN.getObject().getCheck().getCheckId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.INVENTORYBEAN));
            presenter.insertInventoryDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.INVENTORYBEAN));
            presenter.updateInventoryDate(params);
        }
    }

    // 回调方法，从第二个页面回来的时候会执行这个方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case 101:
                list.clear();
                list = (ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>) data.getExtras().getSerializable("goods");
                adapter = new GoodsInventoryAdapter(listview,list,R.layout.item_operationinventory_goods);
                listview.setAdapter(adapter);
                break;
            case 102:
                ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> checkItemListEntities = (ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>) data.getExtras().getSerializable("goods");
                list.addAll(checkItemListEntities);
                adapter = new GoodsInventoryAdapter(listview,list,R.layout.item_operationinventory_goods);
                listview.setAdapter(adapter);
                break;
            default:
                break;
        }
    }

    @Override
    public void insertInventoryDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateInventoryDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
